package it.csi.finregio.finreges.junit.util.parser;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;

import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.rtf.RTFEditorKit;

public class FormatConverter {

	private HTMLDocument tempHTMLDoc;
	private HTMLEditorKit htmlKit;
	private RTFEditorKit rtfKit;

	public FormatConverter() {
		tempHTMLDoc = new HTMLDocument();
		htmlKit = new HTMLEditorKit();
		rtfKit = new RTFEditorKit();
	}

	private String fudge(String strText,int x) {
		String strResult = "";
		StringReader reader = new StringReader(strText);

		try {
			tempHTMLDoc.remove(0, tempHTMLDoc.getLength());
			htmlKit.read(reader, tempHTMLDoc, 0);
			FileOutputStream byteArrayOutputStream = new FileOutputStream("c:\\"+this.getClass().getName()+x+".rtf");
			rtfKit.write(byteArrayOutputStream, tempHTMLDoc, 0, tempHTMLDoc
					.getLength());
			strResult = byteArrayOutputStream.toString();
		} catch (IOException ie) {
		} catch (BadLocationException ble) {
		}

		return strResult;
	}

	public static void main(String args[]) {
		FormatConverter conv = new FormatConverter();
		String strRTF = conv
				.fudge("<P><IMG src=\"http://intratestgbr/announcements/images/1093429553065.jpg\"></P><P>?</P><P>50 <STRONG>pounds</STRONG>, <FONT color=#0000ff>wow</FONT></P>",1);
		System.out.println("RTF: '" + strRTF + "'");
		strRTF = conv
				.fudge("<html><head><p class=default><span style=\"color: #000000\">Description </span><span style=\"color: #000000\"><b>with</b> </span><span style=\"color: #000000\"><i>some</i> </span><span style=\"color: #000000\"><u>formatting</u> </span><span style=\"color: #000000\"></span></p></head></html>",2);
		System.out.println("RTF: '" + strRTF + "'");
		System.exit(0);
	}
}